var that;
class Tab {
    constructor(id) {
        //获取元素
        that = this;
        this.main = document.querySelector(id);
        this.add = this.main.querySelector(".tabadd");
        this.ul = this.main.querySelector('.fisrstnav ul:first-child');
        this.fsection = this.main.querySelector('.tabscon');
        this.init();
    }
    //init 初始化操作让相关的元素绑定事件
    init() {
        this.updataNode();
        this.add.onclick = this.addTab;

        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].index = i;
            this.lis[i].onclick = this.toggleTab;
            this.remove[i].onclick = this.removeTab;
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
        }
    }
    //动态的
    updataNode() {
        this.lis = this.main.querySelectorAll("li");
        this.sections = this.main.querySelectorAll('section');
        this.remove = this.main.querySelectorAll('.guanbi');
        this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child')
    }

    //切换功能
    toggleTab() {
        that.clearClass();
        this.className = 'liactive';
        that.sections[this.index].className = 'conactive';
    }
    clearClass() {
        for (let i = 0; i < this.lis.length; i++) {
            this.lis[i].className = '';
            this.sections[i].className = '';
        }
    }
    //添加功能
    addTab() {
        that.clearClass();
        var random = Math.random();
        var li = '<li class="liactive"><span>新标签页</span><span class="guanbi">×</span></li>';
        var section = "<section class='conactive'>测试" + random + "</section>";
        that.ul.insertAdjacentHTML('beforeend', li);
        that.fsection.insertAdjacentHTML('beforeend', section);
        that.init();
    }
    //删除功能
    removeTab(e) {
        e.stopPropagation(); //阻止冒泡 防止触发li 的点击切换事件
        var index = this.parentNode.index;
        // console.log(index);
        //根据索引号删除对应的li 和section remove（）方法可以删除直接删除指定元素
        that.lis[index].remove();
        that.sections[index].remove();
        that.init();
        //当我们删除的不是选中状态的小li 的时候 原来选中状态的小li 保持不变
        if (document.querySelector('.liactive')) return;
        //当我们删除了选中状态的小li 的时候 让它前一个li做一个点击事件
        index--;
        that.lis[index] && that.lis[index].click();
    }
    //修改功能
    editTab() {
        var str = this.innerHTML;
        // alert(11);
        window.getSelection ? window.getSelection().removeAllRanges() : document.Selection.empty();
        this.innerHTML = '<input type="text" />';
        var input = this.children[0];
        input.value = str;
        input.select(); //文本框里面的文字处于选定状态
        //当我们离开文本框就把文本框里面的值给span
        input.onblur = function () {
            this.parentNode.innerHTML = this.value;
        }
        //按下回车也可以把文本框的值给span
        input.onkeyup = function (e) {
            if (e.keyCode === 13) {
                //手动调用表单失去焦点
                this.blur();
            }
        }
    }
}
new Tab("#tab");